import request from '@/utils/request'
import { getChanels, hasToken, setChannels } from '@/utils/storage'

//保存用户频道数据
export const saveChannelsLis = (payload) => {
  return {
    type: 'home/channels',
    payload,
  }
}
/**
 * 获取用户频道数据
 * @returns
 */
export const getChannelsList = () => {
  return async (dispatch) => {
    // 判断用户是否登录
    if (hasToken()) {
      // 登录了，发送请求获取数据
      const res = await request.get('/user/channels')
      // console.log(res)
      dispatch(saveChannelsLis(res.data.channels))
    } else {
      // 没有登录从本地获取
      const channelss = getChanels()
      // 如果本地有数据,从本地拿
      if (channelss) {
        // 保存到redux中
        dispatch(saveChannelsLis(channelss))
      } else {
        // 本地没有并且用户没有登录，请求接口获取数据
        const res = await request.get('/user/channels')
        dispatch(saveChannelsLis(res.data.channels))
        // 保存到本地一份
        setChannels(res.data.channels)
      }
    }
  }
}

//保存全部列表数据
export const saveAllChannelList = (payload) => {
  return {
    type: 'home/allChannels',
    payload,
  }
}

/**
 * 获取全部频道数据
 * @returns
 */
export const getAllChannellList = () => {
  return async (dispatch) => {
    const res = await request.get('/channels')
    // console.log(res)
    dispatch(saveAllChannelList(res.data.channels))
  }
}

/**
 * 删除频道
 * @param {*} id
 * @returns
 */
export const delChannels = (id) => {
  return async (dispatch, getState) => {
    const { userChannels } = getState().home
    // 如果用户登录了
    if (hasToken()) {
      // 调用接口删除服务器数据
      await request.delete('/user/channels/' + id)
    } else {
      // 没有登录删除本地数据
      const channels = userChannels.filter((item) => item.id !== id)
      // 同步本地
      setChannels(channels)
    }
    // 删除redux数据
    dispatch(saveChannelsLis(userChannels.filter((item) => item.id !== id)))
  }
}

/**
 * 添加频道
 * @param {*} channel
 * @returns
 */
export const addChannel = (channel) => {
  return async (dispatch, getState) => {
    const { userChannels } = getState().home
    if (hasToken) {
      // 请求接口获取数据
      await request.patch('/user/channels', { channels: [channel] })
      dispatch(saveChannelsLis([...userChannels, channel]))
    } else {
      setChannels([...userChannels, channel])
    }
  }
}

//保存文章数据
export const saveArticleList = (payload) => {
  return {
    type: 'home/articleList',
    payload,
  }
}

/**
 * 获取文章数据
 * @param {*} channelId
 * @param {*} timestamp
 * @returns
 */
export const getArticleList = (channelId, timestamp) => {
  return async (dispatch) => {
    const res = await request({
      method: 'get',
      url: '/articles',
      params: {
        timestamp: Date.now(),
        channel_id: channelId,
      },
    })
    // console.log(res)
    dispatch(
      saveArticleList({
        channelId,
        timestamp: res.data.pre_timestamp,
        list: res.data.results,
      })
    )
  }
}

//保存更多的文章数据
export const saveMoreArticleList = (payload) => {
  return {
    type: 'home/moreArticle',
    payload,
  }
}

/**
 * 获取跟多的文章数据
 * @param {*} channelId
 * @param {*} timestamp
 * @returns
 */
export const getMoreArticleList = (channelId, timestamp) => {
  return async (dispatch) => {
    const res = await request({
      method: 'get',
      url: '/articles',
      params: {
        timestamp: timestamp,
        channel_id: channelId,
      },
    })
    console.log(res)
    dispatch(
      saveMoreArticleList({
        channelId,
        timestamp: res.data.pre_timestamp,
        list: res.data.results,
      })
    )
  }
}

export const setMoreAction = (payload) => {
  return {
    type: 'home/moreAction',
    payload,
  }
}
